package com.x.erp.manage;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.x.erp.dao.CarDao;
import com.x.erp.dao.CarOutDao;
import com.x.erp.model.Car;
import com.x.erp.model.CarOut;
import com.x.erp.payload.CarOutMsg;
import com.x.erp.util.ConstantTopic;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.manage.BaseManage;
import com.x.rocketmq.RocketMqProducer;
/**
 * 车辆临时出库服务
 */
@Service
public class CarOutManage extends BaseManage {

	private static final Logger logger = LoggerFactory.getLogger(CarOutManage.class);

	@Autowired
	private CarOutDao carOutDao; // 车辆临时出场dao
	
	@Autowired
	private CarDao carDao; // 车辆dao
	
	@Autowired
	private RocketMqProducer rocketMqProducer;
	
	public void sendMsgCarOutOvertimeTask(CarOut carOut) {
		try {
			List<CarOut> list = carOutDao.selectCarOutListByTime(carOut);
			for (CarOut temp : list) {
				// 发送mq消息
				Car car = new Car();
				car.setCarId(temp.getCarId());
				car = carDao.selectCar(car);
				CarOutMsg carOutMsg = new CarOutMsg();
				carOutMsg.setCarVin(car.getCarVin());
				carOutMsg.setShopName(car.getShop().getShopName());
				carOutMsg.setShopMobile(car.getShop().getShopMobile());
				carOutMsg.setMarketName(car.getMarket().getMarketName());
				produceRocketMqMessage(ConstantTopic.TOPIC_ERP_CAR, ConstantTopic.TAG_CAR_OUT_OVERTIME,
						carOutMsg, "车辆临时出场超过5小时");
				// 发送消息后，将该条数据删除
				carOutDao.delete(temp);
			}
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	private void produceRocketMqMessage(String topic, String tag, Object obj, String msg) {
		try {
			rocketMqProducer.produceMessage(topic, tag, obj);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(msg + "异常", e);
		}
	}
}
